const formartQueryString = () => {
    const {route, options} = getCurrentPages().pop() // options={id:1, name: 'Tom'}

    let path = '/' + route // /pages/xxxx/xxxx

    // 处理查询参数
    const optionsKeys = Object.keys(options) // ['id', 'name']
    if (optionsKeys.length !== 0) {
        const queryString = optionsKeys.reduce((previousValue, currentKey) => {
            // id=1name=Tom& 
            return `${previousValue}${currentKey}=${options[currentKey]}&`
        }, '').slice(0, -1) // id=1&name=Tom

        path += '?' + queryString
    }

    return path
}

export default () =>{
    const checkLogin = wx.getStorageSync('access_token')
    // 如果没有token, 跳转登录页面进行登录
    if(!checkLogin) {
        const path = formartQueryString()
        // 缓存当前的页面路径
        wx.setStorageSync('back_url', path)
        wx.redirectTo({
            url: '/pages/login/login'
        })
    }
}